1 데이터와 선형 회귀모형

예측모형 - 헬로 월드 에서 작업한 데이터와 모델을 가져온다.

library(tidyverse)
library(tidymodels)

library(timetk)
library(modeltime)

# 데이터 ----
full_tbl <- read_rds("data/full_tbl.rds")

# 모형 ----
wkfl_fit_lm <- read_rds("data/wkfl_fit_lm.rds")

1.1 훈련/시험 데이터 분할

예측할 데이터와 모형개발에 활용할 데이터로 나눈 후에 모형개발에 활용할 데이터를 훈련/시험 데이터로 나눈다.

## 예측 데이터와 모형개발 데이터로 분리
forecast_tbl <- full_tbl %>% 
  filter(is.na(확진자))

history_tbl <- full_tbl %>% 
  filter(!is.na(확진자))

## 훈련/시험 데이터 분할

splits <- history_tbl %>% 
  time_series_split(date_var    = 날짜,  
                    assess      = 30,
                    cumulative  = TRUE)
splits
<Analysis/Assess/Total>
<309/30/339>

훈련/시험 데이터로 잘 나눠졌는지 시각적으로 확인한다.

splits %>% 
  tk_time_series_cv_plan() %>% 
  plot_time_series_cv_plan(.date_var = 날짜,
                            .value   = 확진자)

2 피처 공학

tidymodels 생태계의 recipe 팩키지 recipe() 함수를 사용해서 피쳐 공학(feature engineering) 작업을 수행한다. 추후, 다양한 피처를 작업할 예정이라 대략적인 틀만 잡아둔다.

recipe_spec <- recipes::recipe(확진자 ~ ., data = training(splits))

recipe_spec %>% prep() %>%  juice()
# A tibble: 309 x 2
   날짜       확진자
   <date>      <int>
 1 2020-01-23      0
 2 2020-01-24      1
 3 2020-01-25      0
 4 2020-01-26      1
 5 2020-01-27      1
 6 2020-01-28      0
 7 2020-01-29      0
 8 2020-01-30      0
 9 2020-01-31      7
10 2020-02-01      1
# ... with 299 more rows

3

 

데이터 과학자 이광춘 저작

kwangchun.lee.7@gmail.com